<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Invitation
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Invitation extends Model
{
	public $defaultProcess = Q_PROCESS_LITE;
	
	function beforeValidate()
	{
		if (empty($this->validate))
		{
			$this->validate = array(
				'email' => array(
					Valid::NotEmpty  => _("Oops, you forgot to fill in your friend's email"),
					Valid::Email     => _("This email doesn't looks like to be valid"),
				)
			);
		}
		return true;
	}
	
	function beforeSave()
	{
		$this->data['Invitation']['id'] = md5(uniqid());
		$this->data['Invitation']['member_id'] = $this->controller->acl->id;
		return true;
	}
	
	function afterSave()
	{
		$invitations = $this->Member->field('invitations', $this->controller->acl->id, -1);
		
		if ($invitations !== 0 and $invitations > 1)
			return $this->Member->saveField('invitations', $invitations - 1);
		
		return true;
	}
}
?>